package org.ngmj.demo1;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

public class FilesAndPaths {
    public static void main(String[] args) throws IOException {
        String source = "/home/ngmj/下载/jetbra";
        delDir(source);
    }

    private static void delDir(String source) throws IOException {
        Files.walkFileTree(Paths.get(source), new SimpleFileVisitor<>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                Files.delete(file);
                return super.visitFile(file, attrs);
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                Files.delete(dir);
                return super.postVisitDirectory(dir, exc);
            }
        });
    }

    private static void copeDir(String source, String target) throws IOException {
        Files.walk(Paths.get(source)).forEach(path -> {
            try {
                String targetName = path.toString().replace(source, target);
                Path targetPath = Paths.get(targetName);
                if (Files.isDirectory(path)) {
                    Files.createDirectory(targetPath);
                } else if (Files.isRegularFile(path)) {
                    Files.copy(path, targetPath);
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });
    }
}
